<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="artTitle">
        <el-input
          v-model="queryParams.artTitle"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章作者" prop="artAuthor">
        <el-input
          v-model="queryParams.artAuthor"
          placeholder="请输入文章作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章来源" prop="artSource">
        <el-select v-model="queryParams.artSource" placeholder="请选择文章来源" clearable>
          <el-option
            v-for="dict in dict.type.art_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文章类型" prop="artType">
        <el-select v-model="queryParams.artType" placeholder="请选择文章类型" clearable>
          <el-option
            v-for="dict in dict.type.art_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['art:artEssay:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['art:artEssay:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['art:artEssay:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['art:artEssay:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="artEssayList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="文章标题" width="200" show-overflow-tooltip align="center" prop="artTitle"/>
      <el-table-column label="文章分类" width="140" align="center" prop="artCategoryName"/>
      <el-table-column label="文章来源" width="140" align="center" prop="artSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.art_source" :value="scope.row.artSource"/>
        </template>
      </el-table-column>
      <el-table-column label="文章类型" width="140" align="center" prop="artType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.art_type" :value="scope.row.artType"/>
        </template>
      </el-table-column>
      <el-table-column label="文章作者" width="140" align="center" prop="artAuthor"/>
      <el-table-column label="是否展示" width="100" align="center" prop="artShowFlag">
        <template slot-scope="scope">
          {{ scope.row.artShowFlag == 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="广告展示" width="100" align="center" prop="artShowAdv">
        <template slot-scope="scope">
          {{ scope.row.artShowAdv == 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="artPublishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.artPublishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布人" width="120" align="center" prop="artPublishUserName"/>
      <el-table-column label="文章置顶" width="100" align="center" prop="artTop">
        <template slot-scope="scope">
          {{ scope.row.artTop == 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="文章权重" width="100" align="center" prop="artWeight"/>
      <el-table-column label="文章热度" width="100" align="center" prop="artHot"/>
      <el-table-column label="文章喜欢" width="100" align="center" prop="artLike"/>
      <el-table-column label="文章收藏" width="100" align="center" prop="artCollect"/>
      <el-table-column label="文章标签" show-overflow-tooltip width="180" align="center" prop="artTag"/>
      <el-table-column label="状态" width="100" align="center" prop="artStatus"/>
      <el-table-column label="封禁原因" width="180" show-overflow-tooltip align="center" prop="artDissRemark"/>
      <el-table-column label="操作" align="center" width="160" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['art:artEssay:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['art:artEssay:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" :close-on-click-modal="false" append-to-body>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listArtEssay, getArtEssay, delArtEssay, addArtEssay, updateArtEssay} from "@/api/art/artEssay";

export default {
  name: "ArtEssay",
  dicts: ['art_source', 'art_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章管理表格数据
      artEssayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        artSource: null,
        artType: null,
        artCategory: null,
        artCategoryName: null,
        artTitle: null,
        artAuthor: null,
        artAbstract: null,
        artContent: null,
        artMemberContent: null,
        artShowFlag: null,
        artShowAdv: null,
        artShowType: null,
        artPublishTime: null,
        artPublishUser: null,
        artPublishUserName: null,
        artTop: null,
        artWeight: null,
        artHot: null,
        artLike: null,
        artCollect: null,
        artBigImage: null,
        artMinImage: null,
        artMedImage: null,
        artTag: null,
        circleId: null,
        artStatus: null,
        artDissRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文章管理列表 */
    getList() {
      this.loading = true;
      listArtEssay(this.queryParams).then(response => {
        this.artEssayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        artSource: null,
        artType: null,
        artCategory: null,
        artCategoryName: null,
        artTitle: null,
        artAuthor: null,
        artAbstract: null,
        artContent: null,
        artMemberContent: null,
        artShowFlag: null,
        artShowAdv: null,
        artShowType: null,
        artPublishTime: null,
        artPublishUser: null,
        artPublishUserName: null,
        artTop: null,
        artWeight: null,
        artHot: null,
        artLike: null,
        artCollect: null,
        artBigImage: null,
        artMinImage: null,
        artMedImage: null,
        artTag: null,
        circleId: null,
        artStatus: 0,
        artDissRemark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: '/art/editEssay'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.$router.push({
        path: '/art/editEssay',
        query: {
          id: id,
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArtEssay(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArtEssay(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文章管理编号为"' + ids + '"的数据项？').then(function () {
        return delArtEssay(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('art/artEssay/export', {
        ...this.queryParams
      }, `artEssay_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
